package com.easylinkin.linkappapi.photoalbum.util;

import com.easylinkin.linkappapi.common.utils.file.FileExt;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xiaoyang
 * @version 1.0
 * @date 2022/08/19
 * @description
 */
@Slf4j
public class ExportUtil {

  public static FileExt getFileInfoByUrl(String photoUrl,String prefix,String createTime,String fullName,Long count){
    InputStream is = null;
    try {
      // 构造URL
      URL url = new URL(photoUrl);
      // 打开连接
      URLConnection con = url.openConnection();
      //设置请求超时为5s
      con.setConnectTimeout(5 * 1000);
      // 输入流
      is = con.getInputStream();
      FileExt zipFileExt = new FileExt();
      //对url路径做截取
      String folder = prefix + "_" + fullName.split("/")[0] + "_" + createTime;
      String suffix = photoUrl.substring(photoUrl.lastIndexOf("."), photoUrl.length());
      String fileName = folder + "_"+ count + suffix;
      zipFileExt.setFileName(fileName);
      zipFileExt.setInputStream(is);
      zipFileExt.setFolder(folder);
      return zipFileExt;
    } catch (Exception e){
      log.error("下载图片失败：名称【{}】", photoUrl);
    }

    return null;
  }

}
